;
(function() {
	var AchCon = require("../../core/ach/_con");
	var BusCon = require("../../core/bus/_con");
	var UserCon = require("../../core/user/_con");
	var querystring = require('querystring');
	module.exports = {
		prefix: "/extend",
		post: {
			"/add_expiration_time": function() {
				var query = this.request.query;
				var body = this.request.body;
				var ach_id = query.ach_id;
				var goods_id = query.goods_id;
				var result = "fail";;
				(function() {
					if (!ach_id) {
						console.error("参数错误");
						return
					}
					//1、确保订单编号有效
					var ach = AchCon.get(ach_id);
					//2、商家必须是dotnarPay
					if (!(ach && (ach.info.bus_id === "dotnarPay" || ach.info.bus_id === "dotnar_d1") && ach.isEndPay())) {
						console.error("订单编号无效", ach.info.bus_id, ach.isEndPay());
						return
					}
					//3、商品编号必须在这个订单中
					//4、这个订单中对应的商品编号还没有被触发过同样的add_expiration_time事件
					var match_goods_info;
					ach.info.goods_info.some(function(goods_info) {
						console.log(goods_info.goods._id);
						var match_res = (goods_id == goods_info.goods._id && !goods_info.add_expiration_time);
						if (match_res) {
							match_goods_info = goods_info;
							//4.1、锁住，避免有人私底下多次进行POST
							goods_info.add_expiration_time = true;
							ach.save();
						}
						return match_res;
					});
					if (!match_goods_info) {
						console.error("商品编号无效");
						return
					}
					var bus = BusCon.get(ach.info.bus_id);
					//6、这个商品必须有EndPayCallbackBuff，获取BUFF里头的信息
					var cb_url_buff = bus.getGoodsEndPayCallbackBuff(goods_id);
					if (!cb_url_buff) {
						console.error("商品信息错误");
						return;
					}
					var args = cb_url_buff.info.args;
					args = querystring.parse(args);
					var user_id = ach.info.user_id;
					var user = UserCon.get(user_id);
					//7、消费者必须是商家的代理用户，获取消费者对应的商家
					if (!(user && user.bus_id)) {
						console.error("消费者信息错误");
						return;
					}
					console.log(user.bus_id);
					var buy_bus = BusCon.get(user.bus_id);
					var old_expiration_time = buy_bus.expiration_time;
					var expiration_time = new Date(old_expiration_time);
					old_expiration_time = new Date(old_expiration_time);
					var add_year = (~~args.year || 0) * match_goods_info.num;
					var add_month = (~~args.month || 0) * match_goods_info.num;
					var add_date = (~~args.date || 0) * match_goods_info.num
					expiration_time.setFullYear(expiration_time.getFullYear() + add_year);
					expiration_time.setMonth(expiration_time.getMonth() + add_month);
					expiration_time.setDate(expiration_time.getDate() + add_date);
					buy_bus.expiration_time = +expiration_time;
					buy_bus.save();
					result = "success";

					//业务拓展
					var diff_month = expiration_time.getMonth() - old_expiration_time.getMonth();
					var diff_year = expiration_time.getFullYear() - old_expiration_time.getFullYear();
					var diff_total_month = diff_month + diff_year * 12;
					//超过一年
					if (diff_total_month >= 12) { //增值服务，开通推荐建站的功能
						buy_bus.setPerm("can_invite_bus", true);
					}
				}());
				console.log("回调：", query);
				console.log("回调：", body);
				this.body = result;
			}
		}
	}

}());